package com.vtron.dbase.datasource;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 数据源连接池
 * @date : 2022/2/15 17:44
 * @author: 忻龙彪
 */
public class DbPools {
    private static final Logger  LOGGER = LoggerFactory.getLogger(DbPools.class);
    private static final ThreadLocal<DbType>  CONTEXT_HOLDER = new ThreadLocal<DbType>();

    public static void set(DbType dbType) {
        CONTEXT_HOLDER.set(dbType);
    }

    public static DbType get() {
        DbType dbType=  CONTEXT_HOLDER.get();
        if(dbType==null){
            set(DbType.MASTER);
        }
        return CONTEXT_HOLDER.get();
    }

    public static void clear() {
        CONTEXT_HOLDER.remove();
    }
    public static void master() {
        set(DbType.MASTER);
        //LOGGER.info("\n当前数据源：{} 切换主数据源-写入", get());
    }

    public static void slave() {
        set(DbType.SLAVE);
        //LOGGER.info("\n当前数据源：{} 切换从数据源-读出", get());
    }
}
